package com.zyd.bootpage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

//启动页
public class LauncherActivity extends AppCompatActivity {
    public final static String FIRST_LAUNCHER = "first_launcher";
    private final long waitTime = 3000;

    private TextView tvSkip;
    private TextView tvTime;

    private Handler handler;
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        //隐藏状态栏
//        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        fullScreen(this);
        mCountDownTimer.start();
        tvSkip = findViewById(R.id.tv_skip);
        tvTime = findViewById(R.id.tv_time);

        tvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                if (runnable !=null){
                    handler.removeCallbacks(runnable);
                }
            }
        });

        start();

    }

    /**
     * 实现倒计时
     */
    private CountDownTimer mCountDownTimer = new CountDownTimer(waitTime, 1000) {

        //第一个参数为倒计时的总时长

        //第二个参数为倒记的时间间隔
        @Override
        public void onTick(long millisUntilFinished) {
            //此方法每隔1000毫秒会调用一次，millisUntilFinished表示距离结束还有多长时间
//            Toast.makeText(LauncherActivity.this,millisUntilFinished/1000+"s",Toast.LENGTH_SHORT).show();
            tvTime.setText(millisUntilFinished/1000+"S");
        }

        @Override
        public void onFinish() {
            //倒计时结束时可以调用
            Toast.makeText(LauncherActivity.this,"倒时结束",Toast.LENGTH_SHORT).show();
        }

    };


    /**
     * 通过设置全屏，设置状态栏透明
     *
     * @param activity
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明，否则导航栏会呈现系统默认的浅灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                //导航栏颜色也可以正常设置
//                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
//                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }

    /**
     * 利用反射获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    private void start() {
        handler=new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                Intent intent;
                if (isFirstLauncher()) {
                    //true 第二次启动
                    intent = new Intent(LauncherActivity.this, MainActivity.class);
                } else {//false 第一次启动
                    intent = new Intent(LauncherActivity.this, FirstLauncherActivity.class);
                }
                startActivity(intent);

                finish();
            }
        }, waitTime);
    }

    public boolean isFirstLauncher() {
        SharedPreferences sp = getSharedPreferences("guide", Context.MODE_PRIVATE);
        return sp.getBoolean(FIRST_LAUNCHER, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCountDownTimer.cancel();
    }
}